<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
//列表页控制器
class ListController extends Controller{
	public function index(){
		//列表页顶级分类-----------------------------------
		$cateData = Db::table('category')->where("pid=0")->orderBy('csort','ASC')->get();
		//sp($cateData);exit;
		View::with('cateData',$cateData);
		
		//列表页子级分类------------------------------------
		$cid = Q('get.cid',0,'intval');
//		sp($cid);exit;
		//点击所有商品分类  不显示子级分类
		if($cid == 0){
			//默认给数据为空。空就是假。
			$cateSonData = '';
		}else{
			$cateSonData = Db::table('category')->where("pid={$cid}")->get();
		}
//		sp($cateSonData);
		View::with('cateSonData',$cateSonData);
		
		//列表页单个顶级分类:  首页 > 全部商品 > 汽车专区-------------------------
		$cateTopData = Db::table('category')->where("cid={$cid} AND pid=0")->get();
		View::with('cateTopData',$cateTopData);
		
		//点击顶级分类得到 子级分类下面的多有商品----------------------------
		$cateModel = new \Home\Model\Category;
		$goodsData = $cateModel->look($cid);
		
		$data = $cateModel->getAll();
//		sp($data);
//		sp($goodsData);
		
		//一个顶级分类里面的所有商品数----------------------
		$cids = $cateModel->getSon($cateModel->get(),$cid);
//		sp($cids);
		$goodsModel = new \Common\Model\Goods;
		$count = $goodsModel->where("category_cid IN(" . implode(',', $cids) . ")")->count();
		//sp($count);exit;
		View::with('count',$count);
		
		
		//显示商品期数且是商品的最新一期------------------------------
		$periodsModel = new \Common\Model\Periods;
		//sp($goodsDataAll);exit;
		//循环商品数据
		foreach ($goodsData as $k => $v) {
			//select * from goods_periods order by periods desc limit 1;
			//找到期数表里面的goods_gid等于商品gid的，并把periods字段倒序排列截取第一条，并以数组的形式列出来
			//lists('periods') 得到periods的值的数组
			$periodsNums = $periodsModel->orderBy('periods','DESC')->where("goods_gid={$v['gid']}")->limit(1)->lists('periods');
				//$periodsNums存在就是真，就给商品表压入newNum字段作为最新一期
			$goodsData[$k]['newNum'] = $periodsNums[0];
		}
		
		View::with('goodsData',$goodsData);
		
		
		//购物车商品数
		$cartNum = count(isset($_SESSION['cart']['goods']) ? $_SESSION['cart']['goods'] : 0);
		View::with('cartNum',$cartNum);
		
		//子级分类下面的商品
		
		
		
	    View::make();
	}

	public function sonGoods(){
		
		
	}
}








 ?>